/*
<license>
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
</license>
*/
#ifndef GENERIK_TERM_UTILS_H
#define GENERIK_TERM_UTILS_H

/** \file termutils.h
\todo documentation!
*/

#include <sstream>

namespace generik
{

	/**
	\todo documentation!
	*/
	struct progress_bar_counter
	{
		float olddone;
		std::stringstream ps;
	
		progress_bar_counter()
		: olddone(0.0)
		{}
	};
	
	/** Prints a pretty progress bar on the commandline.
	Uses the \r character to send the cursor back to the start of the line,
	in order to redraw the progress bar without printing on a new line.
	\note May not work on all terminals!
	\todo more documentation!
	*/
	inline void progress_bar(int count, int total, const std::string& path, void* object)
	{
		//        [==========] 100% ##############################################################
		const std::string spaces = "                                                              ";
		progress_bar_counter* pc = (progress_bar_counter*)object;
	
		float done = (100.0 * (float)count / (float)total);
		if (done > pc->olddone + 0.1)
		{
			pc->ps.str("");
			pc->ps<<"[";
			for (int i = 0; i < (done/10); ++i) pc->ps<<"=";
			for (int i = (int)(done/10); i < 10-1; ++i) pc->ps<<" ";
			pc->ps<<"]"<<(int)done<<"%";
		}
		std::string outpath = path;
		if (outpath.length() > 63)
		{
			outpath = outpath.substr(0, 63);
		}
		else
		{
			outpath = outpath + spaces.substr(0, 63 - outpath.length());
		}
		std::cerr<<"\r"<<pc->ps.str()<<std::flush;
		std::cerr<<" "<<outpath<<std::flush;
		std::cerr<<std::flush;
		pc->olddone = done;
		
		if (count == total)
		{
			std::cerr<<std::endl;
		}
	}
	
}

#endif



